import vue from 'vue'
import Vuex from 'vuex'
vue.use(Vuex)
import one from './one'
import persistedState from 'vuex-persistedstate'
import index1 from './index1'
import adduser from '../JS/adduser'
export default new Vuex.Store({
    // 存储数据源，相当于data
    state: {
        isBart: false,
        num: 1,
        str: 'string',
        bool: true,
        obj: { name: '刘九日' },
        arr: [1, 2, 3],
        sum: 0,
        newpath: '/app1',
        showData: adduser

    },
    // 计算属性,相当于computed
    getters: {
        // 所依赖的数据源发生变化的时候firtName也会发生变化
        firtName(state) {
            return state.obj.name
        }
    },
    // 直接修改state里面的数据源
    mutations: {
        changeNewpath(state, val) {
            state.newpath = val
        },
        changeSum(state, val) {
            state.sum += val
        },
        changeSum1(state, val) {
            state.sum -= val
        },
        even1(state, val) {
            state.sum += val
        },
        hold(state, val) {
            state.sum += val
        },
        // 添加用户
        showd(state, val) {
            vue.set(state.showData, state.showData.length, val)
        },
        // 删除用户
        dateleuserlist(state, val) {
            for (let index = 0; index < state.showData.length; index++) {
                if (state.showData[index].id == val) {
                    state.showData.splice(index, 1)
                }

            }

        },
        mutaeditList(state, val) {
            for (let index = 0; index < state.showData.length; index++) {
                if (state.showData[index].id == val.id) {
                    vue.set(state.showData, index, val)
                }
            }
        },
        dateleuserlist1(state, val) {
            for (let index = 0; index < state.showarr.length; index++) {
                if (state.showData[index].id == val) {
                    state.showData.splice(index, 1)
                }

            }

        },
    },
    //异步修改state里面的数据源
    actions: {
        even(context, val) {
            context.commit('even1', val)
        },
        wait(context, val) {
            setTimeout(() => {
                context.commit('hold', val)
            }, 1000);
        },
        Acuserlist(content, val) {
            content.commit('showd', val)
        },
        // 删除信息
        Acdeleteuserlist(content, val) {
            content.commit('dateleuserlist', val)
        },
        AceditList(content, val) {
            content.commit('mutaeditList', val)
        },
        Acdeleteuserlist1(content, val) {
            content.commit('dateleuserlist1', val)
        },
    },
    // 模块化vuex
    modules: {
        one,
        indexname1: index1
    },
    // 数据持久化
    plugins: [persistedState()]
})